package com.jingbian.cppccbackend.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingbian.cppccbackend.entity.FileInfo;
import com.jingbian.cppccbackend.entity.MeetingAttachments;
import com.jingbian.cppccbackend.entity.MeetingParticipants;
import com.jingbian.cppccbackend.entity.Meetings;
import com.jingbian.cppccbackend.entity.dto.MeetingsNeedDTO;
import com.jingbian.cppccbackend.mapper.user.MeetingAttachmentsMapper;
import com.jingbian.cppccbackend.mapper.user.MeetingParticipantsMapper;
import com.jingbian.cppccbackend.mapper.user.MeetingsMapper;
import com.jingbian.cppccbackend.service.user.IMeetingsService;
import com.jingbian.cppccbackend.utils.ThreadLocalUtil;
import com.jingbian.cppccbackend.vo.MeetingDetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会议表，用于存储会议基本信息 服务实现类
 * </p>
 *
 * @author pum
 * @since 2025-03-04
 */
@Service
public class MeetingsServiceImpl extends ServiceImpl<MeetingsMapper, Meetings> implements IMeetingsService {
  @Autowired
  private MeetingsMapper meetingsMapper;
  @Autowired
  private MeetingParticipantsMapper meetingParticipantsMapper;
  @Autowired
  private MeetingAttachmentsMapper meetingAttachmentsMapper;

  @Override
  public PageInfo<Meetings> getNeedList(MeetingsNeedDTO meetingsNeedDTO) {
    // 开启分页
    PageHelper.startPage(meetingsNeedDTO.getPageNum(), meetingsNeedDTO.getPageSize());
    Map<String,Object> claims = ThreadLocalUtil.get();
    Integer userId = (Integer) claims.get("uid");
    meetingsNeedDTO.setUid(userId);
    List<Meetings> list = meetingsMapper.getNeedMeetings(meetingsNeedDTO);
    return new PageInfo<>(list);
  }

  @Override
  public PageInfo<Meetings> getNeedList2(MeetingsNeedDTO meetingsNeedDTO) {
    // 开启分页
    PageHelper.startPage(meetingsNeedDTO.getPageNum(), meetingsNeedDTO.getPageSize());
    Map<String,Object> claims = ThreadLocalUtil.get();
    Integer userId = (Integer) claims.get("uid");
    meetingsNeedDTO.setUid(userId);
    List<Meetings> list = meetingsMapper.getNeedMeetings2(meetingsNeedDTO);
    return new PageInfo<>(list);
  }

  @Override
  @Transactional
  public MeetingDetailVO getDetail(Integer mid) {
    Map<String,Object> claims = ThreadLocalUtil.get();
    Integer userId = (Integer) claims.get("uid");
//    return meetingsMapper.getDeail(mid,userId);
    MeetingDetailVO meetingDetailVO = new MeetingDetailVO();
    Meetings meetings = meetingsMapper.selectById(mid);
    BeanUtils.copyProperties(meetings,meetingDetailVO);

    LambdaQueryWrapper<MeetingAttachments> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.eq(MeetingAttachments::getMeetingId,mid);
    List<MeetingAttachments> meetingAttachments1 = meetingAttachmentsMapper.selectList(queryWrapper1);

//    LambdaQueryWrapper<MeetingParticipants> queryWrapper2 = new LambdaQueryWrapper<>();
//    queryWrapper2.eq(MeetingParticipants::getMeetingId,mid);
//    List<MeetingParticipants> meetingAttachments2 = meetingParticipantsMapper.selectList(queryWrapper2);

    /**
     * 查询是否签到状态
     */
    LambdaQueryWrapper<MeetingParticipants> queryWrapper2 = new LambdaQueryWrapper<>();
    queryWrapper2.eq(MeetingParticipants::getMeetingId,mid).eq(MeetingParticipants::getUserId,userId);
    MeetingParticipants meetingAttachments2 = meetingParticipantsMapper.selectOne(queryWrapper2);
    meetingDetailVO.setStatus(meetingAttachments2.getStatus());
    meetingDetailVO.setReason(meetingAttachments2.getReason());

    List<FileInfo> fileInfos = new ArrayList<>();
    for (int i = 0; i < meetingAttachments1.size(); i++) {
      MeetingAttachments meetingAttachments = meetingAttachments1.get(i);
      FileInfo fileInfo = new FileInfo();
      fileInfo.setUrl(meetingAttachments.getFilePath());
      fileInfo.setName("附件"+(i+1));
      fileInfos.add(fileInfo);
    }
    meetingDetailVO.setFileInfo(fileInfos);
    return meetingDetailVO;
  }


  @Transactional
  @Override
  public MeetingDetailVO submitReason(Integer mid, String reason) {
    Map<String,Object> claims = ThreadLocalUtil.get();
    Integer userId = (Integer) claims.get("uid");
    meetingsMapper.setReason(mid,reason,userId);
    return meetingsMapper.getDeail(mid,userId);
  }

  @Override
  @Transactional
  public void sign(Integer mid) {
    Map<String,Object> claims = ThreadLocalUtil.get();
    Integer userId = (Integer) claims.get("uid");
    meetingsMapper.toSign(userId,mid);
  }
}
